package com.sh.tgtools.view;

import android.animation.ArgbEvaluator;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.widget.TextView;


import com.sh.tgtools.R;
import com.sh.tgtools.adapter.GuidePageAdapter;
import com.sh.tgtools.base.BaseActivity;
import com.sh.tgtools.utils.StatusBarUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SplashActivity extends BaseActivity {

    @BindView(R.id.viewpager)
    ViewPager mViewpager;
    @BindView(R.id.tv_tip)
    TextView mTvTip;

    private GuidePageAdapter mAdapter;
    private List<Fragment> mFragmentList = new ArrayList<>();
    private GuideOneFragment mOneFragment;
    private GuideTwoFragment mTwoFragment;
    private GuideTwoFragment mThreeFragment;
    private GuideTwoFragment mFourFragment;
    private SparseArray<int[]> mLayoutViewIdsMap = new SparseArray<int[]>();
    private static final int[] layouts = {R.layout.fragment_guide_one, R.layout.fragment_guide_two, R.layout.fragment_guide_one, R.layout.fragment_guide_two};


    @Override
    protected int getContentView() {
        return R.layout.activity_splash2;
    }

    @Override
    protected void initViews() {
        StatusBarUtils.setColor(this, getResources().getColor(R.color.colorWhite));
        StatusBarUtils.setTextDark(this, true);

        mOneFragment = new GuideOneFragment();
        mFragmentList.add(mOneFragment);


        mTwoFragment = new GuideTwoFragment();

        mFragmentList.add(mTwoFragment);

        mThreeFragment = new GuideTwoFragment();

        mFragmentList.add(mThreeFragment);


        mAdapter = new GuidePageAdapter(getSupportFragmentManager());
        mAdapter.setFragmentList(mFragmentList);
        mViewpager.setAdapter(mAdapter);
        mViewpager.setPageTransformer(true,new ParallaxTransformer(1.2f,0.5f));

        mViewpager.addOnPageChangeListener(new GuidePageChangeListener());
    }

    class ParallaxTransformer implements ViewPager.PageTransformer {

        float parallaxCoefficient;
        float distanceCoefficient;

        public ParallaxTransformer(float parallaxCoefficient, float distanceCoefficient) {
            this.parallaxCoefficient = parallaxCoefficient;
            this.distanceCoefficient = distanceCoefficient;
        }

        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @Override
        public void transformPage(View page, float position) {
            int width = page.getWidth();
            Log.e("page_width",String.valueOf(width));
            Log.e("page_position",String.valueOf(position));
//我们给不同状态的页面设置不同的效果

//通过position的值来分辨页面所处于的状态
            if (position < -1) { //滑出的页面

                page.setScaleX(1f);
                page.setScaleY(1f);

            } else if (position <= 1) { //[-1,1]

                if (position < 0) { //[-1,0]
                    page.setScaleX(1+position);
                    page.setScaleY(1+position);

                } else { //[0,1]
                    page.setScaleX( 1-position);
                    page.setScaleY(  1-position);

                }

            } else { //即将滑入的页面

                page.setScaleX(1f);
                page.setScaleY(1f);

            }
           /* if(position < - 1) { //滑出的页面

                page.setScrollX(( int) (width * 0.75* - 1));

            } else if(position <= 1) { //[-1,1]

                if(position < 0) { //[-1,0]

                    page.setScrollX(( int) (width * 0.75* position));

                } else{ //[0,1]

                    page.setScrollX(( int) (width * 0.75* position));

                }

            } else{ //即将滑入的页面

                page.setScrollX(( int) (width * 0.75));

            }*/
/*
            View rootView = LayoutInflater.from(SplashActivity.this).inflate(layouts[1], null);
            float scrollXOffset = page.getWidth() * parallaxCoefficient;

            ViewGroup pageViewWrapper = (ViewGroup) page;
            if (mLayoutViewIdsMap != null) {
                int[] layer = mLayoutViewIdsMap.get(pageViewWrapper.getChildAt(0).getId());
                if (layer!=null){
                    Log.e("layer", String.valueOf(layer[0]));
                    Log.e("layer", String.valueOf(position));
                    Log.e("pageViewWrapperChild", String.valueOf(pageViewWrapper.getChildAt(0).getId()));
                    Log.e("ViewGroupChild", String.valueOf(((ViewGroup) page).getChildAt(0).getId()));
                }

            }*/

          /*  for (int id : layer) {
                View view = page.findViewById(id);
                if (view != null) {
                    view.setTranslationX(scrollXOffset * position);
                }
                scrollXOffset *= distanceCoefficient;
            }*/
        }
    }

    class GuidePageChangeListener implements ViewPager.OnPageChangeListener {

        ArgbEvaluator mColorEvaluator;

        int mPageWidth, mTotalScrollWidth;

        int mGuideStartBackgroundColor, mGuideEndBackgroundColor;

        String[] mGuideTips;

        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        public GuidePageChangeListener() {
            mColorEvaluator = new ArgbEvaluator();

            mPageWidth = getWindowManager().getDefaultDisplay().getWidth();
            mTotalScrollWidth = mPageWidth * mAdapter.getCount();

            mGuideStartBackgroundColor = getResources().getColor(R.color.color_43C6AC);
            mGuideEndBackgroundColor = getResources().getColor(R.color.color_191654);

            mGuideTips = getResources().getStringArray(R.array.array_guide_tips);
        }

        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            float ratio = (mPageWidth * position + positionOffsetPixels) / (float) mTotalScrollWidth;
            Integer color = (Integer) mColorEvaluator.evaluate(ratio, mGuideStartBackgroundColor, mGuideEndBackgroundColor);
            mViewpager.setBackgroundColor(color);
        }

        @Override
        public void onPageSelected(int position) {
            if (mGuideTips != null && mGuideTips.length > position) {
                mTvTip.setText(mGuideTips[position]);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    }
}
